import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_business_core/business_retails/business_retails_order_details.dart';
import 'package:x3_business_core/business_retails/components/business_retails_order_style_item.dart';
import 'package:x3_component_core/x3_component_core.dart';

class BusinessRetailsOrderItem extends StatefulWidget {
  BusinessRetailsOrderItem({Key key, @required this.styleItem}) : super(key: key);

  final dynamic styleItem;

  @override
  _BusinessRetailsOrderItemState createState() => _BusinessRetailsOrderItemState();
}

class _BusinessRetailsOrderItemState extends State<BusinessRetailsOrderItem> {
  @override
  Widget build(BuildContext context) {
    return _buildStyleItem();
  }

  Widget _buildStyleItem() {
    return BaseCardComponent(
      onTap: () {
        String billsId = BaseUtils.getKeyData(widget.styleItem, 'billsId').toString();
        BaseRouterUtil.getInstance().pushNamed(BusinessRetailsOrderDetails.sName, arguments: {'billsId': billsId});
      },
      child: Column(
        children: [
          _buildOrderItemHeader(),
          BaseComponentUtils.buildHorizontalDivider(),
          _buildOrderItemContent(),
          BaseComponentUtils.buildHorizontalDivider(),
          SizedBox(height: 4.0),
          _buildOrderItemFooter(),
        ],
      ),
    );
  }

  Widget _buildOrderItemHeader() {
    String billsId = BaseUtils.getKeyData(widget.styleItem, 'billsId').toString();
    String saleDate = BaseUtils.getKeyData(widget.styleItem, 'saleDate').toString();
    return Container(
      padding: EdgeInsets.only(bottom: 12.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          BaseComponentUtils.buildTextSpanWidget(['单号:', billsId]),
          Text(BaseTransformUtil.stringToDateTimeStamp(saleDate, isNeedShowSecond: false), style: TextStyle(color: Color(0xFF868686), fontSize: 12)),
        ],
      ),
    );
  }

  Widget _buildOrderItemContent() {
    List<dynamic> items = BaseUtils.getKeyData(widget.styleItem, 'billSubList') ?? [];
    return ListView.separated(
      shrinkWrap: true,
      physics: NeverScrollableScrollPhysics(),
      itemCount: items.length,
      itemBuilder: (BuildContext context, int index) => BusinessRetailsOrderStyleItem(styleItem: items[index]),
      separatorBuilder: (BuildContext context, int index) => BaseComponentUtils.buildHorizontalDivider(),
    );
  }

  Widget _buildOrderItemFooter() {
    String billsNums = BaseUtils.getKeyData(widget.styleItem, 'billsNums').toString();
    String billsSSums = BaseUtils.getKeyData(widget.styleItem, 'billsSSums').toString();
    String accFsums = BaseUtils.getKeyData(widget.styleItem, 'accFsums').toString();
    return Row(
      mainAxisAlignment: MainAxisAlignment.end,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text('共$billsNums件'),
        SizedBox(width: 12.0),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            BaseComponentUtils.buildTextSpanWidget(['结算金额:', '¥$billsSSums']),
            SizedBox(height: 4.0),
            BaseComponentUtils.buildTextSpanWidget(['实销金额:', '¥$accFsums']),
          ],
        )
      ],
    );
  }
}
